/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sample.product;

import org.sample.category.CategoryDAO;
import org.sample.category.CategoryDAOImpl;
import org.sample.discount.DiscountDAO;
import org.sample.discount.DiscountDAOImpl;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Button;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Doublebox;
import org.zkoss.zul.Hbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Row;
import org.zkoss.zul.RowRenderer;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Toolbarbutton;
import org.zkoss.zul.Window;

/**
 *
 * @author hendro
 */
public class ProductGridRowRenderer implements RowRenderer {

    CategoryDAO catDAO = new CategoryDAOImpl();
    DiscountDAO discDAO = new DiscountDAOImpl();
    ProductDAO productDAO = new ProductDAOImpl();
    int no = 1;

    public void render(final Row row, Object data) throws Exception {
        final Product product = (Product) data;
        new Label("" + no++).setParent(row);
        new Label(catDAO.select(product.getCategoryId()).getName()).setParent(row);
        new Label(product.getName()).setParent(row);
        new Label(product.getPrice() + "").setParent(row);
        new Label(discDAO.select(product.getDiscountId()).getName()).setParent(row);

        final Toolbarbutton toolbarbtnUbah = new Toolbarbutton();
        toolbarbtnUbah.setLabel("[ubah]");

        toolbarbtnUbah.addEventListener("onClick", new EventListener() {

            public void onEvent(Event arg0) throws Exception {
                Window window = (Window) row.getRoot();
                Textbox txtboxKeySearch = (Textbox) window.getFellow("txtboxKeySearch");
                txtboxKeySearch.setValue(catDAO.select(product.getCategoryId()).getName());
                Textbox txtboxIdCategory = (Textbox) window.getFellow("txtboxIdCategory");
                txtboxIdCategory.setValue(product.getCategoryId() + "");
                Textbox txtboxProductName = (Textbox) window.getFellow("txtboxProductName");
                txtboxProductName.setValue(product.getName());
                Textbox txtboxProductId = (Textbox) window.getFellow("txtboxProductId");
                txtboxProductId.setValue(product.getId() + "");
                Doublebox dblboxProductPrice = (Doublebox) window.getFellow("dblboxProductPrice");
                dblboxProductPrice.setValue(product.getPrice());
                Combobox cmbboxProductDisc = (Combobox) window.getFellow("cmbboxProductDisc");
                cmbboxProductDisc.setValue(discDAO.select(product.getDiscountId()).getName());
                Textbox txtboxIdDisc = (Textbox) window.getFellow("txtboxIdDisc");
                txtboxIdDisc.setValue(product.getDiscountId() + "");
                Textbox txtboxDesc = (Textbox) window.getFellow("txtboxDesc");
                txtboxDesc.setValue(product.getDescription());
            }
        });

        final Toolbarbutton toolbarbtnHapus = new Toolbarbutton();
        toolbarbtnHapus.setLabel("[hapus]");
        toolbarbtnHapus.addEventListener("onClick", new EventListener() {

            public void onEvent(Event arg0) throws Exception {

                if (Messagebox.show("Apakah data Product: " + product.getName() + " akan dihapus?", "Konfirmasi", Messagebox.YES | Messagebox.NO, null) == Messagebox.YES) {
                    try {
                        productDAO.delete(product);
                    } catch (Exception ex) {
                        Messagebox.show("Gagal menghapus data: " + ex.getMessage());
                    }
                    Window window = (Window) row.getRoot();
                    Button btnBatal = (Button) window.getFellow("btnBatal");
                    toolbarbtnHapus.addForward(null, btnBatal, "onClick");
                }
            }
        });


        Hbox box = new Hbox();
        box.appendChild(toolbarbtnUbah);
        box.appendChild(toolbarbtnHapus);
        box.setParent(row);
    }
}
